package com.daniel.memo;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import com.daniel.database.Database;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;


public class MemoEditActivity extends Activity {
	private Database db;
	
	private EditText title;
	private EditText memo;
	private int memoRowId = -1;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.memo_edit_activity);
        
        db = new Database(this);
        connectInputs();
        
        Bundle extras = getIntent().getExtras();   
        if (extras != null) {
        	memoRowId = extras.getInt(Memo.MEMO_ID);
        	Log.d("memoRowId ","" + memoRowId);
        }
    }

    @Override
    public void onStart() {
    	super.onStart();
    	
    }
    
    @Override 
    public void onStop() {
    	super.onStop();
    }
    
    @Override
    public void onResume() {
    	super.onResume();
    	db.open();
    	if (memoRowId != -1) {
    		pupulateData(memoRowId);
    	}
    }

    @Override
    public void onPause() {
    	super.onPause();
    	db.close();
    }
        
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.edit_memo_menu, menu);
        return true;
    }	

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {	
    	switch(item.getItemId()) {
    		case R.id.edit_memo_menu_save_id:
				if (memoRowId == -1) {
					saveTitleAndBody();
				} else {
					updateMemo();
				}				
				finish();
    			return true;
    			
    		case R.id.edit_memo_menu_cancel_id:
    			finish();
    			return true;
    			
    		default:
    			return super.onOptionsItemSelected(item);
    	}
    }
    
    @Override 
    public void onRestoreInstanceState(Bundle savedInstanceState) {
    	super.onRestoreInstanceState(savedInstanceState);
    	memoRowId = savedInstanceState.getInt(Memo.MEMO_ID);
    	pupulateData(memoRowId);
    }
    
    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
    	super.onSaveInstanceState(savedInstanceState);
    	savedInstanceState.putInt(Memo.MEMO_ID, memoRowId);
    }
    
    private void connectInputs() {
    	//initialize text fields
    	title = (EditText) findViewById(R.id.note_title_id);
    	memo = (EditText) findViewById(R.id.note_body_id);
    }
    
    public void updateMemo() {
    	String textTitle = title.getText().toString();
    	String textBody = memo.getText().toString();

    	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    	Calendar cal = Calendar.getInstance();
    	String date = dateFormat.format(cal.getTime());

    	db.updateMemo(memoRowId, textTitle, textBody, Memo.MEMO_TYPE, date);
    }
    
    public void saveTitleAndBody() {
    	String textTitle = title.getText().toString();
    	String textBody = memo.getText().toString();

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        String date = dateFormat.format(cal.getTime());

        db.insertMemo(textTitle, textBody, Memo.MEMO_TYPE, date);
    }
    
    public void pupulateData(int memoRowId) {
    	Cursor memoCursor = db.fetchMemoById(memoRowId);
   
    	memoCursor.moveToFirst();
    	int numOfRow = memoCursor.getCount();
    	
    	if (numOfRow > 0) {
        	title.setText(memoCursor.getString(0));
        	memo.setText(memoCursor.getString(2));
        }
    }

}
